package com.mma.assigment_registrationform;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	private Button register, login;
	private EditText user, pass;
	private DataManager manager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		manager = DataManager.getInstance();
		initLayout();

	}

	private void initLayout() {
		register = (Button) findViewById(R.id.register);
		login = (Button) findViewById(R.id.login);

		user = (EditText) findViewById(R.id.usernameTF);
		pass = (EditText) findViewById(R.id.passwordTF);

		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String username = user.getText().toString();
				String password = pass.getText().toString();

				if (username != null && password != null) {

					if (manager.isValidUser(username, password)) {
						Intent intent = new Intent(getApplicationContext(),
								LoginScreen.class);
						intent.putExtra("username", username);
						startActivity(intent);
					}
				}

			}
		});

		register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						RegisterScreen.class);
				startActivity(intent);
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
